package Year2012.棋盘放麦子;

/**
 * 64个棋盘格子：1 2 4 8 ....
 * 规律：2的0次方，2的1次方，2的2次方，2的3次方  ....
 * 解法：模拟计算机128位存储
 */
public class 模拟计算机存储解法 {
    static int N = 64+1; // 棋盘格数  --  行数
    static int M = 20; // 结果保存位数  -- 列数
    static int[][] store = new int[N][M];

    public static void main(String[] args) {
        store[0][M-1] = 1;
        fun();
        show1();
        show2();
    }
    public static void show1(){
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                System.out.print(store[i][j]+"\t");
            }
            System.out.println();
        }
    }
    public static void show2(){
        System.out.println("结果：");
        int index = 0;
        for (int i = 0; i < M; i++) {
            if(store[N-1][i] != 0){
                index = i;
                break;
            }
        }
        for (int i = index; i < M; i++) {
            System.out.print(store[N-1][i]);
        }
    }
    public static void fun(){
        int jw = 0;
        for (int i = 1; i <= N-2; i++) {
            jw = 0;
            for (int j = M-1; j >=0; j--) {
                store[i][j] = (jw + store[i-1][j]*2)%10;
                jw          = (jw + store[i-1][j]*2)/10;
            }
        }
        jw = 0;
        for (int i = M-1; i >=0; i--) { // 列
            int sum = 0;
            for (int j = 0; j <= N-2 ; j++) { //行
                sum += store[j][i];
            }
            store[N-1][i] += (sum+jw)%10;
            jw            = (sum+jw)/10;
        }
    }
}
